package com.example.xmppchatter;

import java.util.ArrayList;

import listViewDataStructures.Friend;

import com.example.xmppchatter.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Custom Adapter per popolare la ListView della lista degli amici (FriendList). Estende la classe BaseAdapter.
 * @author giulio
 */
public class AdapterFriendListView extends BaseAdapter {
	
	private ArrayList<Friend> data;
	private Context context;
	
	/**
	 * Costruttore.
	 * @param context - oggetto Context dell'activiy che contiene la lista.
	 * @param data - il riferimento ai dati da inserire nella lista.
	 */
	public AdapterFriendListView(Context context, ArrayList<Friend> data) {
		this.context = context;
		this.data = data;		
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view = convertView;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (convertView == null)
			view = inflater.inflate(R.layout.row_friendlist, null);
		
		// recupero le TextView della lista
		TextView friendName = (TextView) view.findViewById(R.id.textview_friend_name);
		TextView friendStatusMessage = (TextView) view.findViewById(R.id.textview_friend_status_message);
		
		Friend friend = data.get(position);
		
		// Setta i valori nei rispettivi campi
		friendName.setText( friend.getFriendName() );
		friendStatusMessage.setText( friend.getStatusMessage() );
				
		return view;
	}
}
